package com.xored.glance.ui.controls.tree;

import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.xored.glance.ui.controls.decor.StructCell;
import com.xored.glance.ui.controls.decor.StructSource;
import com.xored.glance.ui.controls.tree.content.TreeContent;
import com.xored.glance.ui.sources.ITextBlock;
import com.xored.glance.ui.sources.SourceSelection;

public abstract class TreeStructSource extends StructSource {

	public TreeStructSource(Tree tree) {
		super(tree);
	}

	@Override
	public Tree getControl() {
		return (Tree) super.getControl();
	}

	@Override
	protected StructCell createCell(Item item, int column) {
		return new TreeCell((TreeItem) item, column);
	}

	@Override
	protected abstract TreeContent createContent();

	public SourceSelection getSelection() {
		TreeItem[] items = getControl().getSelection();
		if (items.length > 0) {
			ITextBlock block = content.getContent(createCell(items[0], 0));
			if (block != null) {
				return new SourceSelection(block, 0, block.getText().length());
			}
		}
		return null;
	}

}
